| Conditions | 13 |
| Paths | 8 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like getRenderProperties.js ➔ getRenderProperties often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | /* global HTMLImageElement */ |
||
| 18 | function getRenderProperties(element){ |
||
| 19 | // If the element is a string, query select call again |
||
| 20 | if(typeof element === "string"){ |
||
| 21 | var selector = document.querySelectorAll(element); |
||
| 22 | if(selector.length === 0){ |
||
| 23 | throw new Error("No element found"); |
||
| 24 | } |
||
| 25 | else{ |
||
| 26 | let returnArray = []; |
||
| 27 | for(let i = 0; i < selector.length; i++){ |
||
| 28 | returnArray.push(getRenderProperties(selector[i])); |
||
| 29 | } |
||
| 30 | return returnArray; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | // If element is array. Recursivly call with every object in the array |
||
| 34 | else if(Array.isArray(element)){ |
||
| 35 | let returnArray = []; |
||
| 36 | for(let i = 0; i < element.length; i++){ |
||
| 37 | returnArray.push(getRenderProperties(element[i])); |
||
| 38 | } |
||
| 39 | return returnArray; |
||
| 40 | } |
||
| 41 | // If element, render on canvas and set the uri as src |
||
| 42 | else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLImageElement){ |
||
| 43 | var canvas = document.createElement('canvas'); |
||
| 44 | return { |
||
| 45 | element: canvas, |
||
| 46 | options: getOptionsFromElement(element), |
||
| 47 | renderer: "canvas", |
||
| 48 | afterRender: function(){ |
||
| 49 | element.setAttribute("src", canvas.toDataURL()); |
||
| 50 | } |
||
| 51 | }; |
||
| 52 | } |
||
| 53 | // If SVG |
||
| 54 | else if(typeof SVGElement !== 'undefined' && element instanceof SVGElement){ |
||
| 55 | return { |
||
| 56 | element: element, |
||
| 57 | options: getOptionsFromElement(element), |
||
| 58 | renderer: "svg" |
||
| 59 | }; |
||
| 60 | } |
||
| 61 | // If canvas (in browser) |
||
| 62 | else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLCanvasElement){ |
||
| 63 | return { |
||
| 64 | element: element, |
||
| 65 | options: getOptionsFromElement(element), |
||
| 66 | renderer: "canvas" |
||
| 67 | }; |
||
| 68 | } |
||
| 69 | // If canvas (in node) |
||
| 70 | else if(element.getContext){ |
||
| 71 | return { |
||
| 72 | element: element, |
||
| 73 | renderer: "canvas" |
||
| 74 | }; |
||
| 75 | } |
||
| 76 | else{ |
||
| 77 | throw new Error("Not supported type to render on."); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 82 |